function getParams() {
  var url = window.location.href
    .slice(window.location.href.indexOf("?") + 1)
    .split("&");
  var result = {};
  url.forEach(function(item) {
    var param = item.split("=");
    result[param[0]] = param[1];
  });
  return result;
}

function init() {
  Survey.dxSurveyService.serviceUrl = "";

  var css = {
    root: "sv_main sv_frame sv_default_css"
  };

  var surveyId = decodeURI(getParams()["id"]);
  var model = new Survey.Model({ surveyId: surveyId, surveyPostId: surveyId });
  model.css = css;
  window.survey = model;


survey
    .onDownloadFile
    .add((survey, options) => {
        var xhr = new XMLHttpRequest();
        xhr.responseType = "blob";
        console.log(options)
        xhr.open("GET", "/download?name=" + options.content.content); // "https://surveyjs.io/api/MySurveys/files?name=" + options.content
        xhr.onload = function () {
            var reader = new FileReader();
            reader.onload = function (e) {
                options.callback("success", e.target.result);
            };
            reader.readAsDataURL(new File([xhr.response], options.fileValue.name, {type: options.fileValue.type}));
        };
        xhr.send();
    });

  model.render("surveyElement");
}

init();
